import axios from "axios";

export default () => {
  const downloadFile = (originFileName: string) => {
    const originFileNameArr = originFileName.split('/');
    const fileName = originFileNameArr[originFileNameArr.length - 1];
    axios.get(originFileName, { responseType: "blob" }).then(res => {
      const blob = res.data;
      const reader = new FileReader();
      reader.readAsDataURL(blob);
      reader.onload = (e) => {
        let a = document.createElement('a');
        a.download = fileName;
        (a.href as any) = e.target?.result;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
      }
    })
  }
  return {
    downloadFile,
  }
}